<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="cancleBtn">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <!-- 选项 -->
      <!-- 要显示角色名称，存储角色id label表示要存储的值 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}111</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="okBtn">确定</el-button>
        <el-button size="small" @click="cancleBtn">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 负责存储所有的角色id
      roleIds: []// 负责存储当前所拥有的的角色id
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleList({ page: 1, pagesize: 20 })
      this.list = res.rows
    }, async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds // 赋值本用户的角色
    },
    async  okBtn() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('权限分配成功')
      this.cancleBtn()
    },
    cancleBtn() {
      this.$emit('update:showRoleDialog', false)
      this.roleIds = []
    }
  }
}
</script>

